﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam1.Biz;
using MockPrjTeam1.Entity;
using System.Windows.Forms;

namespace MockPrjTeam1.Pages.Forms
{
    public partial class AddressDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var id = Convert.ToString(Request.QueryString["id"]);
            txtAddress.Focus();
            if (!IsPostBack)
            {
                var biz = new SearchBiz();
                var resultCountry = biz.GetCountry();
                DllCountry.DataTextField = "Country";
                DllCountry.DataValueField = "Country";
                DllCountry.DataSource = resultCountry;
                DllCountry.DataBind();
            }
            if (!string.IsNullOrEmpty(id))
            {
                if (!IsPostBack)
                {
                    var biz = new SearchBiz();
                    var result = new Search();
                    result = biz.GetOne(Convert.ToInt32(id));
                    txtID.Text = id;
                    txtAddress.Text = result.address;
                    txtPostCode.Text = result.postCode;
                    DllCounty.Text = result.county;
                    DllCountry.Text = result.country;
                    DllTown.Text = result.town;
                }
            }
            else
            {
                if (!IsPostBack)
                {
                    DllCountry.Items.Insert(0, "--Select--");
                }
            }
        }
        protected void dllCountry_SelectChanged(object sender, EventArgs e)
        {
            var biz = new SearchBiz();
            var results = biz.GetCounty(Convert.ToString(DllCountry.SelectedValue));
            DllCounty.DataTextField = "County";
            DllCounty.DataValueField = "County";
            DllCounty.DataSource = results;
            DllCounty.DataBind();
            DllCounty.Items.Insert(0, "--Select--");
        }
        protected void dllCounty_SelectChanged(object sender, EventArgs e)
        {
            var biz = new SearchBiz();
            var results = biz.GetTown(Convert.ToString(DllCounty.SelectedValue));
            DllTown.DataTextField = "Town";
            DllTown.DataValueField = "Town";
            DllTown.DataSource = results;
            DllTown.DataBind();
        }
        protected void dllTown_SelectChanged(object sender, EventArgs e)
        {
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var Entity = new Search();            
            Entity.address = Convert.ToString(txtAddress.Text);
            Entity.postCode = Convert.ToString(txtPostCode.Text);
            Entity.town = Convert.ToString(DllTown.SelectedValue);
            Entity.county = Convert.ToString(DllCounty.Text);
            Entity.country = Convert.ToString(DllCountry.Text);

            var biz = new SearchBiz();
            var FK = biz.GetFKAddress(Entity.town);
            Entity.townID = FK.townID;
            Entity.countyID = FK.countyID;
            Entity.countryID = FK.countryID;

            if (!String.IsNullOrEmpty(Convert.ToString(txtID.Text)))
            {
                Entity.addressID = Convert.ToInt32(txtID.Text);
                var result = biz.EditAddress(Entity);
                if (result)
                {
                    MessageBox.Show("Success");
                    Response.Redirect("/Pages/Views/SearchAddress.aspx");
                }
                else
                {
                    MessageBox.Show("Fail");
                }
            }
            else
            {
                var temp = biz.GetIdAddress();
                Entity.addressID = temp;
                var result = biz.CreateAddress(Entity);
                if (result)
                {
                    MessageBox.Show("Success"); 
                    Response.Redirect("/Pages/Views/SearchAddress.aspx");
                }
                else
                {
                    MessageBox.Show("Fail");
                }
            }            
        }
        protected void CustomValidator_PostCode(object source, ServerValidateEventArgs args)
        {
            var ID = txtID.Text;
            if (String.IsNullOrEmpty(ID))
            {
                ID = "0";
            }
            var biz = new SearchBiz();
            var Entity = new Search();
            Entity.addressID = Convert.ToInt32(ID);
            Entity.postCode = Convert.ToString(txtPostCode.Text);
            if (biz.ValidPostCode(Entity.addressID, Entity.postCode))
            {
                args.IsValid = true;
            }
            else
                args.IsValid = false;
                
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/Views/SearchAddress.aspx");
        }
       
    }
}